const apiUrl = 'https://lhappsapi.renbenzhihui.com';
const usercenterUrl = 'https://id.renbenzhihui.com/index.php';
        let countdownTimer = null;

        // 页面加载完成后初始化
        $(function() {
            // 设置当前日期
            const options = { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long' };
            const currentDate = new Date().toLocaleDateString('zh-CN', options);
            $('#current-date').text(currentDate);
        });

        // 登录方式切换
        $('#password-login-tab').on('click', function() {
            $(this).addClass('text-ocean-500 border-ocean-500').removeClass('text-gray-500 border-transparent');
            $('#phone-login-tab').removeClass('text-ocean-500 border-ocean-500').addClass('text-gray-500 border-transparent');
            $('#password-login-form').removeClass('hidden');
            $('#phone-login-form').addClass('hidden');
        });

        //$('#phone-login-tab').on('click', function() {
            $('#phone-login-tab').addClass('text-ocean-500 border-ocean-500').removeClass('text-gray-500 border-transparent');
            $('#password-login-tab').removeClass('text-ocean-500 border-ocean-500').addClass('text-gray-500 border-transparent');
            $('#password-login-form').addClass('hidden');
            $('#phone-login-form').removeClass('hidden');
        //});

        // 密码显示/隐藏切换
        $('#toggle-pwd').on('click', function() {
            const $pwdInput = $('#password');
            const type = $pwdInput.attr('type') === 'password' ? 'text' : 'password';
            $pwdInput.attr('type', type);
            $(this).find('i').toggleClass('fa-eye-slash fa-eye');
        });

        // 手机号输入完成后加载图形验证码
        $('#phone').on('blur', function() {
            const mobile = $(this).val().trim();
            if (/^1[3-9]\d{9}$/.test(mobile)) {
                $('#captcha-image').attr('src', `${apiUrl}/pub/Authcode.html?u=${mobile}&t=${new Date().getTime()}`);
            } else if (mobile) {
                showToast('请输入正确的手机号');
            }
        });

        // 图形验证码刷新
        $('#captcha-image').on('click', function() {
            const mobile = $('#phone').val().trim();
            if (!mobile) {
                showToast('请先输入手机号');
                return;
            }
            $(this).attr('src', `${apiUrl}/pub/Authcode.html?u=${mobile}&t=${new Date().getTime()}`);
        });

        // 用户名/密码输入验证
        $('#username, #password').on('input', function() {
            const username = $('#username').val().trim();
            const password = $('#password').val().trim();
            $('#password-login-action').prop('disabled', !(username && password));
        });

        // 手机号输入验证
        $('#phone').on('input', function() {
            const phone = $(this).val().trim();
            const isPhoneValid = /^1[3-9]\d{9}$/.test(phone);
            $('#get-code-btn').prop('disabled', !isPhoneValid);
            checkPhoneLoginState();
        });

        // 图形验证码输入验证
        $('#graphic-code').on('input', checkPhoneLoginState);

        // 短信验证码输入验证
        $('#code').on('input', checkPhoneLoginState);

        // 检查手机号登录按钮状态
        function checkPhoneLoginState() {
            const phone = $('#phone').val().trim();
            const graphicCode = $('#graphic-code').val().trim();
            const smsCode = $('#code').val().trim();
            
            const isPhoneValid = /^1[3-9]\d{9}$/.test(phone);
            const isGraphicValid = graphicCode.length >= 4;
            const isSmsValid = smsCode.length >= 6;
            
            $('#phone-login-action').prop('disabled', !(isPhoneValid && isGraphicValid && isSmsValid));
        }

        // 获取短信验证码
        $('#get-code-btn').on('click', function() {
            const $btn = $(this);
            const phone = $('#phone').val().trim();
            const graphicCode = $('#graphic-code').val().trim();

            if (!graphicCode) {
                showToast('请输入图形验证码');
                return;
            }

            $btn.prop('disabled', true).text('发送中...');

            // 调用获取短信验证码接口
            $.ajax({
                url: `${usercenterUrl}/sendTool/sendmsg?t=3&auth=${graphicCode}&rt=&mobile=${phone}&jsoncallback="`,
                type: 'GET',
                dataType: 'json',
                success: function(res) {
                    if (res.code/1 === 0) {
                        showToast('短信验证码已发送，请注意查收');
                        startCountdown();
                    } else {
                        showToast(res.message || '获取验证码失败，请重试');
                        $('#captcha-image').click(); // 刷新图形验证码
                        $btn.prop('disabled', false).text('获取验证码');
                    }
                },
                error: function() {
                    showToast('网络异常，请稍后重试');
                    $btn.prop('disabled', false).text('获取验证码');
                }
            });
        });

        // 验证码倒计时
        function startCountdown() {
            let seconds = 60;
            const $btn = $('#get-code-btn');
            
            if (countdownTimer) clearInterval(countdownTimer);
            
            $btn.text(`${seconds}秒后重新获取`).prop('disabled', true);
            
            countdownTimer = setInterval(() => {
                seconds--;
                $btn.text(`${seconds}秒后重新获取`);
                
                if (seconds <= 0) {
                    clearInterval(countdownTimer);
                    $btn.prop('disabled', false).text('获取验证码');
                }
            }, 1000);
        }

        // 手机号登录处理
        $('#phone-login-action').on('click', function() {
            const $btn = $(this);
            const phone = $('#phone').val().trim();
            const smsCode = $('#code').val().trim();
            const img_code = $('#graphic-code').val().trim();

            $btn.prop('disabled', true).text('登录中...');

            $.ajax({
                url: `/Login/LoginAc`,
                type: 'GET',
                dataType: 'json',
                data: {phone:phone,phonevalcode:smsCode},
                success: function(res) {
                    if (res.status/1 == 0) {
                        showToast('登录成功，正在跳转...');
                        console.log(res)
                        localStorage.setItem('token', res.data.token);
                        window.location.href = '/Main/Index';
                    } else {
                        showToast(res.msg || '登录失败，请重试');
                        $btn.prop('disabled', false).text('登录');
                    }
                },
                error: function() {
                    showToast('网络异常，请检查网络后重试');
                    $btn.prop('disabled', false).text('登录');
                }
            });
        });

        // 显示提示消息
        function showToast(message) {
            $('.toast-message').remove();
            
            const toast = $(`
                <div class="toast-message fixed top-4 left-1/2 transform -translate-x-1/2 
                            bg-dark text-white px-4 py-2 rounded-lg shadow-lg z-50 
                            transition-all duration-300 opacity-0 translate-y-[-10px]">
                    ${message}
                </div>
            `);
            $('body').append(toast);
            
            setTimeout(() => {
                toast.css({ opacity: '1', transform: 'translate(-50%, 0)' });
            }, 10);
            
            setTimeout(() => {
                toast.css({ opacity: '0', transform: 'translate(-50%, -10px)' });
                setTimeout(() => toast.remove(), 300);
            }, 3000);
        }